/*
* Copyright (c) 2009 by Thomas Busey and Ruj Akavipat
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Experteyes nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Thomas Busey and Ruj Akavipat ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Thomas Busey and Ruj Akavipat BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
 * SynchronizeJPanel.java  
 *
 * Created on September 10, 2007, 11:49 AM
 */

package eyetrackercalibrator.gui;

import eyetrackercalibrator.framemanaging.FrameManager;
import eyetrackercalibrator.framemanaging.ScreenFrameManager;
import eyetrackercalibrator.gui.util.AnimationTimer;
import java.awt.event.ActionListener;

/**
 * To use this Panel.  The "start" method must be explicitly called after 
 * initialization is completed.  Before disposing of this panel, "stop" method
 * must be called to stop animation timer from continue firing.
 * @author  eeglab
 */
public class SynchronizeJPanel extends javax.swing.JPanel {
    
    private AnimationTimer timer;    
    private FrameManager eyeFrameManager = null;
    private FrameManager screenFrameManager = null;

    
    /** Creates new form SynchronizeJPanel */
    public SynchronizeJPanel() {
        initComponents();
        
        // Set up animation timer
        timer = new AnimationTimer();
        timer.setDisplayJPanel(displayJPanel);
        timer.setEyeFrameScrollingJPanel(eyeFrameScrollingJPanel);
        timer.setScreenFrameScrollingJPanel(screenFrameScrollingJPanel);
    }
    
    /** For starting animation */
    public void start(){
        timer.start();
    }
    
    /** For stoping animation */
    public void stop(){
        timer.stop();
    }
    
    public int getEyeViewCurrentFrame() {
        return eyeFrameScrollingJPanel.getCurrentFrame();
    }
    
    public void setEyeViewCurrentFrame(int eyeViewCurrentFrame) {
        eyeFrameScrollingJPanel.setCurrentFrame(eyeViewCurrentFrame);
    }
    
    public int getScreenViewCurrentFrame() {
        return screenFrameScrollingJPanel.getCurrentFrame();
    }
    
    public void setScreenViewCurrentFrame(int screenViewCurrentFrame) {
        screenFrameScrollingJPanel.setCurrentFrame(screenViewCurrentFrame);
    }
    
    public FrameManager getEyeFrameManager() {
        return eyeFrameManager;
    }
    
    public void setEyeFrameManager(FrameManager eyeFrameManager) {
        this.eyeFrameManager = eyeFrameManager;
        // Set total frame for control
        eyeFrameScrollingJPanel.setTotalFrame(eyeFrameManager.getTotalFrames());
        timer.setEyeFrameManager(eyeFrameManager);
    }
    
    public FrameManager getScreenFrameManager() {
        return screenFrameManager;
    }
    
    public void setScreenFrameManager(ScreenFrameManager screenFrameManager) {
        this.screenFrameManager = screenFrameManager;
        // Set total frame for control
        screenFrameScrollingJPanel.setTotalFrame(screenFrameManager.getTotalFrames());
        timer.setScreenFrameManager(screenFrameManager);
    }    
    
    /**
     * Add actionlistener to process "Synchronize" and "Back" commands
     */
    public void addActionListener(ActionListener listener){
        synchronizeButton.addActionListener(listener);
        cancelButton.addActionListener(listener);
    }    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        displayJPanel = new eyetrackercalibrator.gui.DisplayJPanel();
        synchronizeButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        eyeFrameScrollingJPanel = new eyetrackercalibrator.gui.FrameScrollingJPanel();
        screenFrameScrollingJPanel = new eyetrackercalibrator.gui.FrameScrollingJPanel();

        synchronizeButton.setText("Synchronize");

        cancelButton.setText("Cancel");

        jPanel1.setLayout(new java.awt.GridLayout(1, 0, 5, 0));
        jPanel1.add(eyeFrameScrollingJPanel);
        jPanel1.add(screenFrameScrollingJPanel);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(synchronizeButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 666, Short.MAX_VALUE)
                .add(cancelButton))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 806, Short.MAX_VALUE)
                .addContainerGap())
            .add(displayJPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 826, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(displayJPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(synchronizeButton)
                    .add(cancelButton)))
        );
    }// </editor-fold>//GEN-END:initComponents
            
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private eyetrackercalibrator.gui.DisplayJPanel displayJPanel;
    private eyetrackercalibrator.gui.FrameScrollingJPanel eyeFrameScrollingJPanel;
    private javax.swing.JPanel jPanel1;
    private eyetrackercalibrator.gui.FrameScrollingJPanel screenFrameScrollingJPanel;
    private javax.swing.JButton synchronizeButton;
    // End of variables declaration//GEN-END:variables
    
}
